package gomz.pres.question;


import gomz.pres.CheckBoxList;
import gomz.pres.GUIMain;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

/**
 * Diese Klasse erstellt das GUI der Uebersicht in der Fragensicht
 *
 * @author Dilji
 */

public class GUIQOverview {

	// all the variables for main panels
	private JPanel plOverview;
	private JPanel plOverviewNord;
	private JScrollPane spOverview;

	// all the variables for plOverview
	private JLabel lbOcategory;
	private CheckBoxList liOcategory;
	private JScrollPane spOcategory;
	private JLabel lbOque;
	private JList liOque;
	private JScrollPane spOque;
	private JLabel lbOcon;
	private JTextArea taOcon;
	private JScrollPane spOcon;
	private JButton btOedit;
	private JButton btOdel;
	private JButton btOnew;
	private JLabel lbOsearch;
	private ImageIcon imgSearch;
	private ImageIcon imgSearchActive;
	private ImageIcon imgSearchCan;
	private ImageIcon imgSearchCanActive;
	private JButton btIconSearch;
	private JButton btIconSearchCan;
	private JTextField tfSearch;
	private Box searchBox;

	/**
	 * Erstellt das GUI der Uebersicht in der Home-Ansicht
	 * @param categorieList fuellt die Liste der Kategorien
	 * @param questionList fuellt die Liste der Fragen
	 * @return gibt das GUI zurueck
	 */
	public JScrollPane getTab(String[] categorieList, String[] questionList) {

		// initialize the panels, add to scrollpane and set position
		this.plOverview = new JPanel();
		this.plOverviewNord = new JPanel();
		this.spOverview = new JScrollPane(plOverviewNord);
		this.plOverviewNord.setLayout(new BorderLayout());
		this.plOverviewNord.setBorder(new EmptyBorder((2 * GUIMain.getAbstand()), 0,	GUIMain.getAbstand(), 0));
		this.plOverviewNord.add(plOverview, BorderLayout.NORTH);

		// set background of main panels
		this.plOverview.setBackground(Color.WHITE);
		this.plOverviewNord.setBackground(Color.WHITE);

		// ********************************************************************************
		// PANEL OVERVIEW
		// ********************************************************************************

		// initialize the components of overview
		this.lbOcategory = new JLabel("Kategorie:");
		this.liOcategory = new CheckBoxList(categorieList);
		this.spOcategory = new JScrollPane(liOcategory);
		this.lbOque = new JLabel("<html><body>Alle Fragen:<br>[P] - Public (\u00d6ffentlich)</body></html>");
		this.liOque = new JList(questionList);
		this.spOque = new JScrollPane(liOque);
		this.lbOcon = new JLabel("Inhalt der Frage:");
		this.taOcon = new JTextArea("Inhalt");
		this.spOcon = new JScrollPane(taOcon);
		this.btOedit = new JButton("Editieren");
		this.btOdel = new JButton("L\u00f6schen");
		this.btOnew = new JButton("Neue Frage");
		
		// initialize search field
		this.lbOsearch = new JLabel("Titel suchen:");
		this.imgSearch = new ImageIcon(getClass().getResource("/gomz/img/search.png"));
		this.imgSearch.setImage(imgSearch.getImage().getScaledInstance(28, 28, Image.SCALE_DEFAULT));
		this.imgSearchActive = new ImageIcon(getClass().getResource("/gomz/img/search_active.png"));
		this.imgSearchActive.setImage(imgSearchActive.getImage().getScaledInstance(imgSearch.getIconWidth(), imgSearch.getIconWidth(), Image.SCALE_DEFAULT));
		
		// button search
		this.btIconSearch = new JButton();
		this.btIconSearch.setToolTipText("Frage suchen");
		this.btIconSearch.setIcon(imgSearch);
		this.btIconSearch.setPressedIcon(imgSearchActive);
		this.btIconSearch.setBackground(null);
		this.btIconSearch.setBorder(null);
		this.btIconSearch.setBorderPainted(false);
		this.btIconSearch.setPreferredSize(new Dimension(imgSearch.getIconWidth(), imgSearch.getIconHeight()));
		this.btIconSearch.setMaximumSize(new Dimension(imgSearch.getIconWidth(), imgSearch.getIconHeight()));
			// kein background beim klicken des icon
		this.btIconSearch.setContentAreaFilled(false);
			// keine dotted line wenn ausgew�hlt
		this.btIconSearch.setFocusable(false);
		// button search cancel
		this.imgSearchCan = new ImageIcon(getClass().getResource("/gomz/img/searchCan.png"));
		this.imgSearchCan.setImage(this.imgSearchCan.getImage().getScaledInstance(15, 15, Image.SCALE_DEFAULT));
		this.imgSearchCanActive = new ImageIcon(getClass().getResource("/gomz/img/searchCan_active.png"));
		this.imgSearchCanActive.setImage(this.imgSearchCanActive.getImage().getScaledInstance(15, 15, Image.SCALE_DEFAULT));
		this.btIconSearchCan = new JButton();
		this.btIconSearchCan.setToolTipText("Suche zur\u00fccksetzten");
		this.btIconSearchCan.setIcon(imgSearchCan);
		this.btIconSearchCan.setPressedIcon(imgSearchCanActive);
		this.btIconSearchCan.setBackground(null);
		this.btIconSearchCan.setBorder(null);
		this.btIconSearchCan.setBorderPainted(false);
			// kein background beim klicken des buttons
		this.btIconSearchCan.setContentAreaFilled(false);
			// keine dotted line wenn ausgew�hlt
		this.btIconSearchCan.setFocusable(false);
		// TextField search
		this.tfSearch = new JTextField();
		this.tfSearch.setPreferredSize(new Dimension(437, GUIMain.getTfheight()));
		this.tfSearch.setMaximumSize(new Dimension(437, GUIMain.getTfheight()));
		this.tfSearch.setBorder(GUIMain.getBorderInGap());
		this.tfSearch.setBackground(null);
		
		// BOX for searchfield
		Box searchFieldBox = Box.createHorizontalBox();
		searchFieldBox.setBorder(GUIMain.getBorderColor());
		searchFieldBox.add(tfSearch);
		searchFieldBox.add(Box.createHorizontalStrut((GUIMain.getAbstand()) / 3));
		searchFieldBox.add(btIconSearchCan);
		searchFieldBox.add(Box.createHorizontalStrut((GUIMain.getAbstand()) / 3));
		
		// BOX for search
		this.searchBox = Box.createHorizontalBox();
		searchBox.add(searchFieldBox);
		searchBox.add(Box.createHorizontalStrut((GUIMain.getAbstand()) / 3));
		searchBox.add(btIconSearch);

		// list single selection
		this.liOque.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		// format taOcon
		// true to set formatting
		this.taOcon.setOpaque(true);
		// user can not edit this JTextArea
		this.taOcon.setEditable(false);
		// set automatically linewrap
		this.taOcon.setLineWrap(true);
		// set wrap style word
		this.taOcon.setWrapStyleWord(true);
		// set font
		this.taOcon.setFont(this.lbOcon.getFont());
		// set font color
		this.taOcon.setForeground(GUIMain.getFontcolorinactive());
		
		// size of scrollpane from textarea
		this.spOcon.setPreferredSize(new Dimension(500, 150));
		this.spOcon.setMaximumSize(new Dimension(500, 150));
		this.spOcon.setBorder(BorderFactory.createCompoundBorder(GUIMain.getBorderColor(), GUIMain.getBorderInGapScrollPane()));
		this.spOcon.setBackground(null);

		// size of scrollpane from list
		this.spOque.setPreferredSize(new Dimension(500, 150));
		this.spOque.setMaximumSize(new Dimension(500, 150));
		this.spOque.setBorder(GUIMain.getBorderColor());
		this.spOque.setBackground(null);
		
		// size of scrollpane from category
		this.spOcategory.setPreferredSize(new Dimension(500, 100));
		this.spOcategory.setMaximumSize(new Dimension(500, 100));
		this.spOcategory.setBorder(GUIMain.getBorderColor());
		this.spOcategory.setBackground(null);

		// size of buttons
		this.btOedit.setPreferredSize(new Dimension(GUIMain.getBtWidth(), GUIMain.getBtHeight()));
		this.btOedit.setMaximumSize(new Dimension(GUIMain.getBtWidth(), GUIMain.getBtHeight()));
		this.btOdel.setPreferredSize(new Dimension(GUIMain.getBtWidth(), GUIMain.getBtHeight()));
		this.btOdel.setMaximumSize(new Dimension(GUIMain.getBtWidth(), GUIMain.getBtHeight()));
		this.btOnew.setPreferredSize(new Dimension(GUIMain.getBtWidth(),GUIMain.getBtHeight()));
		this.btOnew.setMaximumSize(new Dimension(GUIMain.getBtWidth(), GUIMain.getBtHeight()));
		
		// set font for new question button
		this.btOnew.setFont(this.btOnew.getFont().deriveFont(Font.BOLD));
		
		// BOX
		Box btObox = Box.createHorizontalBox();
		btObox.add(btOedit);
		btObox.add(Box.createHorizontalStrut(GUIMain.getAbstand()));
		btObox.add(btOdel);
		btObox.add(Box.createHorizontalStrut((GUIMain.getAbstand()*5)));
		btObox.add(btOnew);
		
		// set speed of scroll
		this.spOcategory.getVerticalScrollBar().setUnitIncrement(GUIMain.getScrollspeed());
		this.spOcon.getVerticalScrollBar().setUnitIncrement(GUIMain.getScrollspeed());
		this.spOque.getVerticalScrollBar().setUnitIncrement(GUIMain.getScrollspeed());
		this.spOverview.getVerticalScrollBar().setUnitIncrement(GUIMain.getScrollspeed());

		// position of components
		this.plOverview.setLayout(new GridBagLayout());
		GridBagConstraints co = new GridBagConstraints();
		co.fill = GridBagConstraints.HORIZONTAL; // in x-Achse fuellen
		co.insets = new Insets(GUIMain.getAbstand(), GUIMain.getAbstand(), 0, GUIMain.getAbstand());
		co.anchor = GridBagConstraints.NORTHWEST; // Ausrichtung oben
		
		co.gridx = 0; // Spalte 1
		co.gridy = 0; // Zeile 1
		//plOverview.add(lbOsearch, co); // label Suchen
		co.gridx = 1; // Spalte 2
		co.gridy = 0; // Zeile 1
		//plOverview.add(, co); // Button neu
		
		co.gridx = 0; // Spalte 1
		co.gridy = 1; // Zeile 2
		plOverview.add(lbOsearch, co); // label Suchen
		co.gridx = 1; // Spalte 2
		co.gridy = 1; // Zeile 2
		plOverview.add(this.searchBox, co); // Suchfeld
		
		co.gridx = 0; // Spalte 1
		co.gridy = 2; // Zeile 3
		plOverview.add(lbOcategory, co); // label Kategorie
		co.gridx = 1; // Spalte 2
		co.gridy = 2; // Zeile 3
		plOverview.add(spOcategory, co); // ComboBox Kategorie

		co.gridx = 0; // Spalte 1
		co.gridy = 3; // Zeile 4
		plOverview.add(lbOque, co); // Label Frage
		co.gridx = 1; // Spalte 2
		co.gridy = 3; // Zeile 4
		plOverview.add(spOque, co); // Liste Fragen

		co.gridx = 0; // Spalte 1
		co.gridy = 4; // Zeile 5
		plOverview.add(lbOcon, co); // Label Inhalt der Frage
		co.gridx = 1; // Spalte 2
		co.gridy = 4; // Zeile 5
		plOverview.add(spOcon, co); // TextArea Inhalt der Frage
		
		co.gridx = 0; // Spalte 1
		co.gridy = 5; // Zeile 6
		//plOverview.add(, co); // Com
		co.gridx = 1; // Spalte 2
		co.gridy = 5; // Zeile 6
		plOverview.add(btObox, co); // Buttons

		return (spOverview);

	}

	// ********************************************************************************
	// Getters und Setters
	// ********************************************************************************
	
	public JTextArea getTaOcon() {
		return taOcon;
	}

	public void setTaOcon(JTextArea taOcon) {
		this.taOcon = taOcon;
	}

	public JButton getBtOedit() {
		return btOedit;
	}

	public void setBtOedit(JButton btOedit) {
		this.btOedit = btOedit;
	}

	public JButton getBtOdel() {
		return btOdel;
	}

	public void setBtOdel(JButton btOdel) {
		this.btOdel = btOdel;
	}

	public JButton getBtOnew() {
		return btOnew;
	}

	public void setBtOnew(JButton btOnew) {
		this.btOnew = btOnew;
	}

	public JButton getBtIconSearch() {
		return btIconSearch;
	}

	public void setBtIconSearch(JButton btIconSearch) {
		this.btIconSearch = btIconSearch;
	}

	public JButton getBtIconSearchCan() {
		return btIconSearchCan;
	}

	public void setBtIconSearchCan(JButton btIconSearchCan) {
		this.btIconSearchCan = btIconSearchCan;
	}

	public JTextField getTfSearch() {
		return tfSearch;
	}

	public void setTfSearch(JTextField tfSearch) {
		this.tfSearch = tfSearch;
	}

	public CheckBoxList getLiOcategory() {
		return liOcategory;
	}

	public void setLiOcategory(CheckBoxList liOcategory) {
		this.liOcategory = liOcategory;
	}

	public JList getLiOque() {
		return liOque;
	}

	public void setLiOque(JList liOque) {
		this.liOque = liOque;
	}
}
